﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Reflection;
using System;

namespace HareGame
{
    public class ConfigBuild : Editor
    {
        [MenuItem("HareGame/生成配置文件", false, 1)]
        public static void BuildConfig()
        {
            Config config = new Config();
            FieldInfo[] infos = config.GetType().GetFields();
            foreach (var info in infos)
            {
                Type type = info.FieldType;
                string path = Application.streamingAssetsPath + "/Configs/" + type.Name + ".json";
                object obj = type.Assembly.CreateInstance(type.FullName);
                if (File.Exists(path))
                {
                    string oldJson = File.ReadAllText(path);
                    obj = JsonUtility.FromJson(oldJson, type);
                }
                string json = JsonUtility.ToJson(obj);
                File.WriteAllText(path, json);
            }
        }

        public static string enumBase =
    @"using System;

namespace HareGame
{
 [Serializable]
 public enum #EnumName#
 {
   None = #Num#,
   #Data#
 }
}
 ";

        #region DataManagerCode
        public static string DataManagerBase =
            @"
using QFramework;
using UnityEngine;
using System.Collections.Generic;

namespace HareGame
{
    [MonoSingletonPath(""GameEntry/DataManager"")]
    public partial class DataManager : MonoBehaviour, ISingleton
        {
            /// <summary>
            /// 初始化
            /// </summary>
            public void OnSingletonInit()
            {
      *Data*
            }

            /// <summary>
            /// 定义单例
            /// </summary>
            public static DataManager Instance
            {
                get { return MonoSingletonProperty<DataManager>.Instance; }
            }


            public T GetData<T>(string ID, List<T> Datas) where T : BaseData
            {
                T data = null;
                foreach (var v in Datas)
                {
                    if (v.ID == ID)
                    {
                        data = v;
                        break;
                    }
                }

                if (data == null)
                {
                    LogKit.W(""找不到ID为<color=red>"" + ID + ""</color>的"" + typeof(T).Name + ""数据"");
                }

                return data;
            }
        }
    }
";
        #endregion

        [MenuItem("HareGame/生成Excel的数据", false, 1)]
        public static void BuildExcelJsonAndEnum()
        {
            string dataManagerCode = "";
            string[] excelFiles = Directory.GetFiles(Application.dataPath + "/Excel/", "*.xlsx");
            foreach (var file in excelFiles)
            {
                string code = Excel2Json.Convert(file);
                string fileName = file.Substring(file.LastIndexOf('/') + 1, file.LastIndexOf('.') - file.LastIndexOf('/') - 1);
                if (!string.IsNullOrEmpty(code))
                {
                    dataManagerCode += code + "\n";
                    string enumClass = enumBase;
                    enumClass = enumClass.Replace("#EnumName#", fileName + "ID");
                    enumClass = enumClass.Replace("#Num#", "0");
                    List<BaseData> datas = Util.Instance.ReadJsonDataFromStreamAssets<BaseData>(fileName);
                    string data = "";
                    foreach (var v in datas)
                    {
                        data += v.ID + ",\n";
                    }

                    enumClass = enumClass.Replace("#Data#", data);

                    File.WriteAllText(Application.dataPath + "/Game/Scripts/Datas/Enums/" + fileName + "ID.cs",
                        enumClass);
                }
            }

            string manager = DataManagerBase;
            manager = manager.Replace("*Data*", dataManagerCode);
            File.WriteAllText(Application.dataPath + "/Game/Scripts/Datas/Managers/DataManager.cs", manager);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
}
